package com.mmm.station.security.session;


import com.mmm.station.security.constant.Constant;
import com.mmm.station.security.entity.PermissionInfo;
import com.mmm.station.security.exception.SecuretyException;
import com.mmm.station.security.utils.ContextHolderUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;

/**
 * Created by mmm on 2016/12/6.
 */
public class SessionUtils {

    private final Logger logger = LogManager.getLogger(this.getClass());






    private static SessionUtils instance;
    private SessionUtils (){}
    public static  SessionUtils getInstance() {
        if (instance == null) {
            instance = new SessionUtils();
        }
        return instance;
    }




    /**
     * 设置用户的信息
     *
     * @param authInfo
     */
    public void setAuthInfo(Object object) {
        if (object != null) {
            ContextHolderUtils.getSession().setAttribute(Constant.AUTHINFO, object);
        } else {
            throw new SecuretyException("实体类不能为空");
        }

    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public Object getAuthInfo() {
        return ContextHolderUtils.getSession().getAttribute(Constant.AUTHINFO);
    }

    /**
     * 设置用户权限
     *
     * @param permissionInfo
     */
    public void setPermission(PermissionInfo permissionInfo) {

        ContextHolderUtils.getSession().setAttribute(Constant.PERMISSIONINFO, permissionInfo);
    }

    /**
     * 清除用户权限
     */
    public void clearPermission() {
        ContextHolderUtils.getSession().removeAttribute(Constant.PERMISSIONINFO);
    }

    /**
     * 获取用户的角色
     *
     * @return
     */
    public List<String> getRoles() {
        PermissionInfo permissionInfo = (PermissionInfo) ContextHolderUtils.getSession().getAttribute(Constant.PERMISSIONINFO);
        if (permissionInfo !=null){
            return permissionInfo.getRoles();
        }
        return null;


    }

    /**
     * 获取用户的权限
     *
     * @return
     */
    public List<String> getPermissions() {
        PermissionInfo permissionInfo = (PermissionInfo) ContextHolderUtils.getSession().getAttribute(Constant.PERMISSIONINFO);
        if (permissionInfo != null){
            return permissionInfo.getPermissions();
        }
        return null;
    }


}
